package com.czn.shopdemo.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.czn.shopdemo.model.Business;
import com.czn.shopdemo.model.Cart;
import com.czn.shopdemo.model.Goods;
import com.czn.shopdemo.model.Result;
import com.czn.shopdemo.model.vo.GoodsVO;
import com.czn.shopdemo.service.CartService;
import com.czn.shopdemo.service.GoodsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @author Zenan Chen
 * @date 2023/4/4 14:51
 */
@RestController
@RequestMapping("/cart")
@Api(tags = "购物车管理")
public class CartController {

    private static final Logger log = LoggerFactory.getLogger(CartController.class);

    @Autowired
    private CartService cartService;

    @ApiOperation("查询购物车商品的详情信息")
    @PostMapping("/getCartGoodsDetail")
    public Result getCartGoodsDetail(@RequestParam String uuid) {
        try {
            log.info("接受到参数：uuid:{}", uuid);
            log.info("------------------------->/getCartGoodsDetail 查询购物车商品的详情信息开始");
            Cart cart = cartService.getCartGoodsDetail(uuid);
            log.info("------------------------->/getCartGoodsDetail 查询购物车商品的详情信息结束");
            return Result.ok(cart);
        } catch (Exception e) {
            log.error("------------------------->/getCartGoodsDetail 查询购物车商品的详情信息列表异常【{}】", e.getMessage());
            return Result.build(201, e.getMessage());
        }
    }

    @ApiOperation("删除购物车里的商品")
    @PostMapping("/deleteCartGoods")
    public Result deleteCartGoods(@RequestParam String cartId) {
        try {
            log.info("接受到参数：cartId:{}", cartId);
            log.info("------------------------->/deleteCartGoods 删除购物车里的商品开始");
            cartService.deleteCartGoods(cartId);
            log.info("------------------------->/deleteCartGoods 删除购物车里的商品结束");
            return Result.ok("删除购物车里的商品成功！");
        } catch (Exception e) {
            log.error("------------------------->/deleteCartGoods 删除购物车里的商品异常【{}】", e.getMessage());
            return Result.build(201, e.getMessage());
        }
    }

    @ApiOperation("查询购物车商品列表")
    @PostMapping("/getCartGoodsList")
    public Result getCartGoodsList(@RequestParam Integer pageNo, @RequestParam Integer pageSize, HttpServletRequest request) {
        try {
            log.info("接受到参数：pageNo:{},pageSize:{}", pageNo, pageSize);
            log.info("------------------------->/getCartGoodsList 查询购物车商品列表开始");
            Page<Cart> result = cartService.getCartGoodsList(pageNo, pageSize, request);
            log.info("------------------------->/getCartGoodsList 查询购物车商品列表结束");
            return Result.ok(result);
        } catch (Exception e) {
            log.error("------------------------->/getCartGoodsList 查询购物车商品列表异常【{}】", e.getMessage());
            return Result.build(201, e.getMessage());
        }
    }

    @ApiOperation("添加商品到购物车")
    @PostMapping("/addToCart")
    public Result addToCart(@RequestBody Cart cart, HttpServletRequest request) {
        try {
            log.info("接受到参数：goods:{}", JSONObject.toJSONString(cart));
            log.info("------------------------->/addToCart 添加商品到购物车开始");
            cartService.addToCart(cart, request);
            log.info("------------------------->/addToCart 添加商品到购物车结束");
            return Result.ok("添加商品到购物车成功！");
        } catch (Exception e) {
            log.error("------------------------->/addToCart 添加商品到购物车异常【{}】", e.getMessage());
            return Result.build(201, e.getMessage());
        }
    }

}
